<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>电影数据分析 - Netflix数据可视化</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1400px;
            margin: 0 auto;
        }
        
        h1 {
            text-align: center;
            color: white;
            margin-bottom: 30px;
            font-size: 2.5em;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }
        
        .controls {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 30px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }
        
        .controls h2 {
            margin-bottom: 15px;
            color: #333;
        }
        
        .model-buttons {
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
        }
        
        .model-btn {
            padding: 12px 24px;
            border: none;
            border-radius: 25px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 3px 10px rgba(0,0,0,0.2);
        }
        
        .model-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.3);
        }
        
        .model-btn.active {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
        }
        
        .chart-container {
            background: white;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
            min-height: 500px;
        }
        
        #chartTitle {
            text-align: center;
            font-size: 24px;
            margin-bottom: 20px;
            color: #333;
        }
        
        #chart {
            width: 100%;
            height: 500px;
        }
        
        .loading {
            text-align: center;
            padding: 50px;
            font-size: 18px;
            color: #666;
        }
        
        .error {
            text-align: center;
            padding: 50px;
            font-size: 18px;
            color: #f5576c;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🎬 电影数据分析 - Netflix数据可视化</h1>
        
        <div class="controls">
            <h2>选择分析模型：</h2>
            <div class="model-buttons">
                <button class="model-btn" onclick="loadModel('yearDistribution')">📅 年份分布分析</button>
                <button class="model-btn" onclick="loadModel('typeDistribution')">🎭 类型分布分析</button>
                <button class="model-btn" onclick="loadModel('countryDistribution')">🌍 国家分布分析</button>
                <button class="model-btn" onclick="loadModel('ratingDistribution')">⭐ 评级分布分析</button>
                <button class="model-btn" onclick="loadModel('genreDistribution')">🎪 流派分布分析</button>
            </div>
        </div>
        
        <div class="chart-container">
            <h3 id="chartTitle">请选择一个分析模型</h3>
            <div id="chart"></div>
        </div>
    </div>

    <script src="../admin/components/echarts/echarts.min.js"></script>
    <script>
        let currentChart = null;
        
        // 初始化图表
        function initChart() {
            const chartDom = document.getElementById('chart');
            if (currentChart) {
                currentChart.dispose();
            }
            currentChart = echarts.init(chartDom);
            return currentChart;
        }
        
        // 加载模型数据
        function loadModel(modelType) {
            const chart = initChart();
            const chartTitle = document.getElementById('chartTitle');
            
            // 更新按钮状态
            document.querySelectorAll('.model-btn').forEach(btn => {
                btn.classList.remove('active');
            });
            event.target.classList.add('active');
            
            // 显示加载状态
            chartTitle.innerHTML = '加载中...';
            chart.showLoading();
            
            // API映射
            const apiMap = {
                'yearDistribution': '/api/admin/netflix/analysis/yearDistribution',
                'typeDistribution': '/api/admin/netflix/analysis/typeDistribution',
                'countryDistribution': '/api/admin/netflix/analysis/countryDistribution',
                'ratingDistribution': '/api/admin/netflix/analysis/ratingDistribution',
                'genreDistribution': '/api/admin/netflix/analysis/genreDistribution'
            };
            
            // 调用API
            fetch(apiMap[modelType], {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                chart.hideLoading();
                if (data.code === 1) {
                    chartTitle.innerHTML = data.response.title;
                    renderChart(chart, modelType, data.response);
                } else {
                    chartTitle.innerHTML = '数据加载失败';
                    console.error('API错误:', data.message);
                }
            })
            .catch(error => {
                chart.hideLoading();
                chartTitle.innerHTML = '数据加载失败';
                console.error('请求失败:', error);
            });
        }
        
        // 渲染图表
        function renderChart(chart, modelType, data) {
            let option = {};
            
            switch(modelType) {
                case 'yearDistribution':
                    option = getLineChartOption(data.years, data.counts, '数量');
                    break;
                case 'typeDistribution':
                    option = getPieChartOption(data.types, data.counts);
                    break;
                case 'countryDistribution':
                    option = getBarChartOption(data.countries, data.counts, '数量');
                    break;
                case 'ratingDistribution':
                    option = getPieChartOption(data.ratings, data.counts);
                    break;
                case 'genreDistribution':
                    option = getBarChartOption(data.genres, data.counts, '数量');
                    break;
            }
            
            chart.setOption(option);
            
            // 响应式调整
            window.addEventListener('resize', function() {
                chart.resize();
            });
        }
        
        // 折线图配置
        function getLineChartOption(xData, yData, yAxisName) {
            return {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '10%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    data: xData,
                    axisLabel: {
                        rotate: 45,
                        interval: Math.floor(xData.length / 20)
                    }
                },
                yAxis: {
                    type: 'value',
                    name: yAxisName
                },
                series: [{
                    data: yData,
                    type: 'line',
                    smooth: true,
                    lineStyle: {
                        width: 3,
                        color: '#667eea'
                    },
                    areaStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: 'rgba(102, 126, 234, 0.5)'
                        }, {
                            offset: 1,
                            color: 'rgba(102, 126, 234, 0.1)'
                        }])
                    },
                    itemStyle: {
                        color: '#667eea'
                    }
                }],
                dataZoom: [{
                    type: 'inside'
                }, {
                    type: 'slider'
                }]
            };
        }
        
        // 饼图配置
        function getPieChartOption(names, values) {
            const data = names.map((name, index) => ({
                name: name,
                value: values[index]
            }));
            
            return {
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}: {c} ({d}%)'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left'
                },
                series: [{
                    type: 'pie',
                    radius: ['40%', '70%'],
                    avoidLabelOverlap: true,
                    itemStyle: {
                        borderRadius: 10,
                        borderColor: '#fff',
                        borderWidth: 2
                    },
                    label: {
                        show: true,
                        formatter: '{b}: {d}%'
                    },
                    emphasis: {
                        label: {
                            show: true,
                            fontSize: '20',
                            fontWeight: 'bold'
                        }
                    },
                    data: data,
                    color: ['#667eea', '#764ba2', '#f093fb', '#f5576c', '#4facfe', 
                            '#00f2fe', '#43e97b', '#38f9d7', '#fa709a', '#fee140']
                }]
            };
        }
        
        // 柱状图配置
        function getBarChartOption(xData, yData, yAxisName) {
            return {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '10%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    data: xData,
                    axisLabel: {
                        rotate: 45,
                        interval: 0
                    }
                },
                yAxis: {
                    type: 'value',
                    name: yAxisName
                },
                series: [{
                    data: yData,
                    type: 'bar',
                    itemStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#667eea'
                        }, {
                            offset: 1,
                            color: '#764ba2'
                        }]),
                        borderRadius: [5, 5, 0, 0]
                    },
                    emphasis: {
                        itemStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                offset: 0,
                                color: '#f093fb'
                            }, {
                                offset: 1,
                                color: '#f5576c'
                            }])
                        }
                    }
                }]
            };
        }
        
        // 页面加载时自动加载第一个模型
        window.onload = function() {
            // 默认不加载，等用户点击
        };
    </script>
</body>
</html>
